package com.ruoyi.info.userController;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.info.domain.FhInfoSigninlist;
import com.ruoyi.info.domain.FhInfoUser;
import com.ruoyi.info.domain.SignInDto;
import com.ruoyi.info.domain.SignInVo;
import com.ruoyi.info.service.IFhInfoSigninlistService;
import com.ruoyi.info.service.IFhInfoUserService;
import com.ruoyi.tokenTools.UserLoginToken;
import com.test.aop.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 签到列表Controller
 * 
 * @author ruoyi
 * @date 2024-11-09
 */
@Api(tags = "用户签到列表")
@RestController
@RequestMapping("/info/userSigninlist")
public class UserFhInfoSigninlistController extends BaseController
{
    @Autowired
    private IFhInfoSigninlistService fhInfoSigninlistService;

    @Autowired
    private IFhInfoUserService iFhInfoUserService;


    /**
     * 今日签到
     */
    @Anonymous
    @UserLoginToken
    @ApiOperation("今日签到")
    @Log(title = "今日签到", businessType = BusinessType.UPDATE)
    @PutMapping("/toadysign")
    public R<String> todaySign( )
    {
        try{
            Date singinDate = new Date();
            //获取用户信息，修改积分
            FhInfoUser fhInfoUser = new FhInfoUser();
            Long userId = UserThreadLocal.getUser();
            fhInfoUser.setUserId(userId);
            List<FhInfoUser> fhInfoUsers = iFhInfoUserService.selectFhInfoUserList(fhInfoUser);
            FhInfoUser infoUser = fhInfoUsers.get(0);
            Long points = fhInfoUsers.get(0).getPoints();
            //比对这次和上次签到的日期是否相邻，相邻的话连续签到日期加一
            //查询上次的签到日期，和连续签到天数
            FhInfoSigninlist fhInfoSigninlist = new FhInfoSigninlist();
            fhInfoSigninlist.setUserId(userId);
            List<FhInfoSigninlist> fhInfoSigninlists = fhInfoSigninlistService.selectFhInfoSigninlistList(fhInfoSigninlist);
            //判断用户是否连续签到
            FhInfoSigninlist fhInfoSigninlist1 = fhInfoSigninlists.get(0);
            Calendar aCalendar = Calendar.getInstance();
            aCalendar.setTime(fhInfoSigninlist1.getSinginDate());
            int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
            aCalendar.setTime(singinDate);
            int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
            //连续签到
            if((day2-day1)==1){
                if(fhInfoSigninlist1.getSinginDay()%3==0){
                    points = points+30;
                }else {
                    points= points+10;
                }
                Long singinDay = fhInfoSigninlist1.getSinginDay();
                singinDay+=1;
                //修改用户表积分
                infoUser.setPoints(points);
                iFhInfoUserService.updateFhInfoUser(fhInfoUser);
                //修改签到表
                fhInfoSigninlist1.setSinginDay(singinDay);
                fhInfoSigninlist1.setSinginDate(singinDate);
                fhInfoSigninlistService.updateFhInfoSigninlist(fhInfoSigninlist1);
                return R.ok("签到成功");
            }
            //没有连续签到,积分加10，连续签到日设置为1
            points+=10;
            //修改用户表积分
            infoUser.setPoints(points);
            iFhInfoUserService.updateFhInfoUser(infoUser);
            //修改签到表
            fhInfoSigninlist1.setSinginDay(1L);
            fhInfoSigninlist1.setSinginDate(singinDate);
            fhInfoSigninlistService.updateFhInfoSigninlist(fhInfoSigninlist1);
            return R.ok("签到成功");
        }catch (Exception e){
            return R.fail(e.getMessage());
        }

    }

    /**
     * 查询签到列表列表
     */
    @Anonymous
    @UserLoginToken
    @ApiOperation("查询签到列表列表")
    @GetMapping("/list")
    public R<SignInDto>  list()
    {
        try{
            SignInDto signInDto = new SignInDto();
            Long integral = 10L;
            List<SignInVo> list = new ArrayList<>(7);

            FhInfoSigninlist fhInfoSigninlist = new FhInfoSigninlist();
            fhInfoSigninlist.setUserId(UserThreadLocal.getUser());
            List<FhInfoSigninlist> FhInfoSigninlist = fhInfoSigninlistService.selectFhInfoSigninlistList(fhInfoSigninlist);
            if(FhInfoSigninlist.isEmpty()){
                //没有签到过
                fhInfoSigninlist.setSinginDay(0L);
                fhInfoSigninlist.setSinginDate(new Date());
                fhInfoSigninlistService.insertFhInfoSigninlist(fhInfoSigninlist);
                //没有签到过
                for (int i = 0; i < 8; i++) {
                    list.add(new SignInVo(i,0,integral,1));
                }
                signInDto.setUserSignInNum(0L);
                signInDto.setSignInVoList(list);
                return R.ok(signInDto);
            }else {
                // 签过到
                Long continueDay = FhInfoSigninlist.get(0).getSinginDay();
                int continueDays = continueDay.intValue();
                signInDto.setUserSignInNum(continueDay);
                // 1, 前六天的 flag是要固定的
                if (continueDays <= 6) {
                    for (int i = 1; i < 8; i++) {
                        int multiple = 1;
                        if(i%3==0){
                            multiple = 3;
                        }else {
                            multiple = 1;
                        }
                        if (i <= continueDays) {
                            list.add(new SignInVo(i, 1,integral,multiple));
                        } else {
                            list.add(new SignInVo(i, 0,integral,multiple));
                        }
                    }
                } else {
                    // 2, 6天后的签到天数要跟随日期增加
                    for (int i = 5; i > -2; i--) {
                        int multiple = 1;
                        if((continueDays - i)%3==0){
                            multiple = 3;
                        }else {
                            multiple = 1;
                        }
                        if (i > -1) {
                            list.add(new SignInVo(continueDays - i, 1,integral,multiple));
                        } else {
                            list.add(new SignInVo(continueDays + 1, 0,integral,multiple));
                        }
                    }
                }
            }
            signInDto.setSignInVoList(list);
            return R.ok(signInDto);
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    //定时更新用户签到表，每天更新一次,更新签到表
    @Scheduled(cron = "0 0 0 1/1 * ? ")
    public void updateSigninList(){
        //比对这次和上次签到的日期是否相邻，相邻的话连续签到日期加一
        //查询上次的签到日期，和连续签到天数
        FhInfoSigninlist fhInfoSigninlist = new FhInfoSigninlist();
        List<FhInfoSigninlist> fhInfoSigninlists = fhInfoSigninlistService.selectFhInfoSigninlistList(fhInfoSigninlist);
        for (int i = 0; i < fhInfoSigninlists.size(); i++) {
            Calendar aCalendar = Calendar.getInstance();
            aCalendar.setTime(fhInfoSigninlists.get(i).getSinginDate());
            int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
            aCalendar.setTime(new Date());
            int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
            //连续签到
            if((day2-day1)!=1){
                //修改签到表
                fhInfoSigninlists.get(i).setSinginDay(0L);
                fhInfoSigninlistService.updateFhInfoSigninlist(fhInfoSigninlists.get(i));
            }
        }
    }

}
